package com.heyige.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heyige.entity.MemberLevel;
import com.heyige.mapper.MemberLevelMapper;
import com.heyige.service.MemberLevelService;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service("memberLevelService")
public class MemberLevelServiceImpl extends ServiceImpl<MemberLevelMapper, MemberLevel> implements MemberLevelService {
    
    private final MemberLevelMapper memberLevelMapper;
    
    public MemberLevelServiceImpl(MemberLevelMapper memberLevelMapper) {
        this.memberLevelMapper = memberLevelMapper;
    }
    
    @Override
    public Optional<MemberLevel> findByIdAndIsActiveTrue(Long id) {
        LambdaQueryWrapper<MemberLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberLevel::getId, id)
                .eq(MemberLevel::getIsActive, true);
        return Optional.ofNullable(memberLevelMapper.selectOne(queryWrapper));
    }
    
    @Override
    public Page<MemberLevel> findByIsActiveTrueOrderBySortOrderAsc(Page<MemberLevel> page) {
        LambdaQueryWrapper<MemberLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberLevel::getIsActive, true)
                .orderByAsc(MemberLevel::getSortOrder);
        return memberLevelMapper.selectPage(page, queryWrapper);
    }
}